package com.telit.firecommand.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.telit.common.entity.PageData;
import com.telit.common.util.MinioUtils;
import com.telit.firecommand.dao.CameraDownloadLogDao;
import com.telit.firecommand.dao.CommCameraInfoDao;
import com.telit.firecommand.entity.CameraDownloadLog;
import com.telit.firecommand.entity.CommCameraInfo;
import com.telit.firecommand.service.CameraDownloadLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author zhouyu
 * @description 历史视频下载实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
@RequiredArgsConstructor
public class CameraDownloadLogServiceImpl implements CameraDownloadLogService {

    @Autowired
    private CameraDownloadLogDao cameraDownloadLogDao;

    private final CommCameraInfoDao commCameraInfoDao;

    @Override
    public PageData<Object> queryByCondition(Integer page, Integer rows, String cameraName, String startTime,
                                             String endTime) {
        if (cameraName != null && !"".equals(cameraName)) {
            cameraName = "%" + cameraName + "%";
        }
        IPage<CameraDownloadLog> iPage = new Page<>(page, rows);
        IPage<CameraDownloadLog> pages = cameraDownloadLogDao.queryByCondition(iPage, cameraName, startTime, endTime);
        List<CameraDownloadLog> records = pages.getRecords();
        records.forEach(e -> {
            String videoUrl = e.getVideoUrl();
            if (videoUrl != null) {
                e.setVideoUrl(MinioUtils.previewImg(videoUrl));
            }
        });
        PageData<Object> pageData = new PageData<>();
        pageData.setDatas(records);
        pageData.setCount((int) pages.getTotal());
        pageData.setCurrent((int) pages.getCurrent());
        pageData.setSize((int) pages.getSize());
        return pageData;
    }

    @Override
    public PageData<Object> queryByConditionWithDistrictCode(String districtCode, Integer page, Integer rows,
                                                             String cameraName, String startTime, String endTime) {
        final List<String> names = ObjectUtil.defaultIfNull(ChainWrappers.lambdaQueryChain(commCameraInfoDao)
                        .select(CommCameraInfo::getName)
                        .likeRight(CommCameraInfo::getDistrictCode, districtCode)
                        .list(),
                CollUtil.<CommCameraInfo>newArrayList()
        ).parallelStream().map(CommCameraInfo::getName).distinct().collect(Collectors.toList());
        log.info("区域code:[{}],对应的视频监控名称如下:[{}]", districtCode, JSONUtil.toJsonPrettyStr(names));
        if (CollUtil.isEmpty(names)) {
            return new PageData<Object>(0, 1, 10, null);
        }
        if (cameraName != null && !"".equals(cameraName)) {
            cameraName = "%" + cameraName + "%";
        }
        IPage<CameraDownloadLog> iPage = new Page<>(page, rows);
        IPage<CameraDownloadLog> pages = cameraDownloadLogDao.queryByConditionWithNames(names, iPage, cameraName,
                startTime, endTime);
        List<CameraDownloadLog> records = pages.getRecords();
        records.forEach(e -> {
            String videoUrl = e.getVideoUrl();
            if (videoUrl != null) {
                e.setVideoUrl(MinioUtils.previewImg(videoUrl));
            }
        });
        PageData<Object> pageData = new PageData<>();
        pageData.setDatas(records);
        pageData.setCount((int) pages.getTotal());
        pageData.setCurrent((int) pages.getCurrent());
        pageData.setSize((int) pages.getSize());
        return pageData;
    }

    @Override
    public CameraDownloadLog queryById(Long id) {
        return cameraDownloadLogDao.selectByPrimaryKey(id);
    }

}
